package com.bsoft.gol.hcs.specialist.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import com.bsoft.gol.hcs.specialist.bean.request.QueryDoctorFollowupReq;
import com.bsoft.gol.hcs.specialist.entity.FollowUpSubsmoduleEntity;
import com.bsoft.gol.hcs.utils.CommonUtils;

import java.util.Date;
import java.util.List;

/**
 * @author zhengcb10708
 * @describe
 * @create 2020-08-12 23:03
 */
public abstract class FollowUpSubsmoduleDao extends HibernateSupportDelegateDAO<FollowUpSubsmoduleEntity> {
    @DAOMethod(sql = " select count(1) from FollowUpSubsmoduleEntity t where t.isEnable='1' and  t.organizationId=:organizationId    and t.doctorId=:doctorId ")
    public abstract Long getMySubScriptionTypeCount(@DAOParam("organizationId") String organizationId, @DAOParam("departmentId") String departmentId, @DAOParam("doctorId") String doctorId);

    @DAOMethod(sql = "update  FollowUpSubsmoduleEntity t set t.subScriptionType=:subScriptionType ,t.lastModifyDt=:now  where t.isEnable='1' and t.organizationId=:organizationId   and t.doctorId=:doctorId ")
    public abstract void updateMySubScriptionType(@DAOParam("now") Date now,
                                                  @DAOParam("subScriptionType") String subScriptionType,
                                                  @DAOParam("organizationId") String  organizationId,
                                                  @DAOParam("departmentId") String departmentId,
                                                  @DAOParam("doctorId") String doctorId);

    public String qryMySubScriptionType(final QueryDoctorFollowupReq request) {
        HibernateStatelessResultAction<String> action =
                new AbstractHibernateStatelessResultAction<String>() {
                    @Override
                    public void execute(StatelessSession ss) throws Exception {
                        StringBuilder sqlBuilder = new StringBuilder();
                        sqlBuilder.append(" select t.subScriptionType FROM follow_up_subsmodule t where t.isEnable='1' and t.organization_id= :organizationId  " );
                       // sqlBuilder.append(" and t.department_id=:departmentId  ");
                        sqlBuilder.append(" and t.doctor_id=:doctorId  ");
                        sqlBuilder.append(" ORDER BY t.moduleId desc limit 1  ");
                        Query query = ss.createSQLQuery(sqlBuilder.toString());
                        query.setParameter("organizationId", request.getOrganizationId());
                      //  query.setParameter("departmentId", request.getDepartmentId());
                        query.setParameter("doctorId", request.getDoctorId());
                        List list = query.list();
                        String  result = null;
                        if (CommonUtils.isNotEmpty(list)) {
                            result = (String) list.get(0);
                        }
                        setResult(result);
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
}
